<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Threadtype;
use App\Models\Typeoption;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class ThreadtypeController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('分类');
            $content->description('管理');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑');
            $content->description('分类');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('添加');
            $content->description('分类');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Threadtype::class, function (Grid $grid) {

            $grid->id('ID')->sortable();

            $grid->column('order','显示排序')->editable();
            $grid->column('name','名称');
            $grid->column('description','简单描述');
            $grid->typevar('字段')->pluck('title')->label();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Threadtype::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('name', '名称');
            $form->text('description', '简单描述');
            $form->image('icon', 'LOGO');

            $form->switch('special', '分类状态');
            $form->number('order', '排序')->default(1);

            $form->text('order', '显示排序');
            $form->hasMany('typevars','已有字段', function (Form\NestedForm $form) {
                $form->select('option_id', '字段')->options(Typeoption::all()->pluck('title','id'));
                $form->switch('required','必填');
                $form->switch('unchangeable','不可修改');
                $form->switch('search','表单搜索');
                $form->number('displayorder','排序')->default(1);
            });
        });
    }
}
